package com.ruoyi.service.Impl;

import com.ruoyi.mapper.CompetitionMapper;
import com.ruoyi.service.CompetitionService;
import com.ruoyi.pojo.ResultCompetition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author kaka.huang
 * @for study
 */
@Service
public class CompetitionServiceImpl implements CompetitionService {
    @Autowired
    private CompetitionMapper competitionMapper;

    /*左侧数据接口，只拿取top值为空的数据*/
    @Override
    public Map<String, Object> selectLCompetition() {
        List<ResultCompetition> resultCompetitions = competitionMapper.selectCompetitionMapper();

        // 第一步：根据 game_id 分组得出总数量 total
        long totalGameIdCount = resultCompetitions.stream()
                .map(ResultCompetition::getGame_id)
                .distinct()
                .count();

        // 第二步：筛出 top 为空的数据
        List<ResultCompetition> filteredCompetitions = resultCompetitions.stream()
                .filter(competition -> competition.getTop() == null)
                .collect(Collectors.toList());

        // 第三步：按照 game_id 分组，并将 name 整合
        Map<String, ResultCompetition> groupedCompetitions = new HashMap<>();

        for (ResultCompetition competition : filteredCompetitions) {
            String gameId = competition.getGame_id();
            if (groupedCompetitions.containsKey(gameId)) {
                ResultCompetition existingCompetition = groupedCompetitions.get(gameId);
                String mergedNames = existingCompetition.getName() + "、" + competition.getName();
                existingCompetition.setName(mergedNames);
            } else {
                groupedCompetitions.put(gameId, new ResultCompetition(
                        competition.getGame_id(),
                        competition.getGame_time(),
                        competition.getName(),
                        competition.getGame_name(),
                        competition.getGame_result(),
                        competition.getCertificate(),
                        competition.getTop()
                ));
            }
        }

        // 将分组后的数据转换为列表
        List<ResultCompetition> finalCompetitions = new ArrayList<>(groupedCompetitions.values());

        // 构建返回的 Map
        Map<String, Object> result = new HashMap<>();
        result.put("competitions", finalCompetitions);
        result.put("total", totalGameIdCount);
        return result;
    }

    /*右侧数据接口，只拿取top值不为空的数据*/
    @Override
    public Map<String, Object> selectRCompetition() {
        List<ResultCompetition> resultCompetitions = competitionMapper.selectCompetitionMapper();
        // 第一步：筛出 top 不为空的数据
        List<ResultCompetition> filteredCompetitions = resultCompetitions.stream()
                .filter(competition -> competition.getTop() != null)
                .collect(Collectors.toList());

        // 第二步：按照 game_id 分组，并将 name 整合
        Map<String, ResultCompetition> groupedCompetitions = new HashMap<>();

        for (ResultCompetition competition : filteredCompetitions) {
            String gameId = competition.getGame_id();
            if (groupedCompetitions.containsKey(gameId)) {
                ResultCompetition existingCompetition = groupedCompetitions.get(gameId);
                String mergedNames = existingCompetition.getName() + "、" + competition.getName();
                existingCompetition.setName(mergedNames);
            } else {
                groupedCompetitions.put(gameId, new ResultCompetition(
                        competition.getGame_id(),
                        competition.getGame_time(),
                        competition.getName(),
                        competition.getGame_name(),
                        competition.getGame_result(),
                        competition.getCertificate(),
                        competition.getTop()
                ));
            }
        }

        // 将分组后的数据转换为列表
        List<ResultCompetition> finalCompetitions = new ArrayList<>(groupedCompetitions.values());

        // 按照 top 值升序排序
        finalCompetitions.sort(Comparator.comparingInt(ResultCompetition::getTop));

        // 构建返回的 Map
        Map<String, Object> result = new HashMap<>();
        result.put("competitions", finalCompetitions);
        return result;
    }
}
